// projects/oa/pages/project_resource/research_resource_detail.js
const cloudHelper = require('../../../config/project_helpers_config').cloudHelper;
const pageHelper = require('../../../config/project_helpers_config').pageHelper;
const timeHelper = require('../../../config/project_helpers_config').timeHelper;
const ResearchResourceBiz = require('../../../config/path_resolver.js').pageHelper;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoad: false,
    resource: null,
    projectId: '', // 如果从项目页面进入，会有项目ID
    resourceId: '',
    
    // 选项卡状态
    tabCur: 0,
    tabs: ['基本信息', '引用关系', '版本比较']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!options.id) {
      pageHelper.showModal('参数错误', '缺少资源ID');
      return;
    }

    this.setData({
      resourceId: options.id,
      projectId: options.projectId || ''
    });

    this._loadDetail();
  },

  /**
   * 加载资源详情
   */
  async _loadDetail() {
    try {
      const params = {
        id: this.data.resourceId
      };

      const resource = await cloudHelper.callCloudData('project_resource/detail', params);
      
      // 处理数据
      if (resource.ADD_TIME) {
        resource.ADD_TIME_DISPLAY = timeHelper.fmtDateTimeCN(resource.ADD_TIME);
      }
      if (resource.EDIT_TIME) {
        resource.EDIT_TIME_DISPLAY = timeHelper.fmtDateTimeCN(resource.EDIT_TIME);
      }
      
      this.setData({
        resource,
        isLoad: true
      });

      wx.setNavigationBarTitle({
        title: '资源详情: ' + resource.RESOURCE_NAME
      });
    } catch (err) {
      console.error('[资源详情] 加载失败:', err);
      pageHelper.showModal('加载失败', '获取资源详情出错');
    }
  },

  /**
   * 选项卡切换
   */
  switchTab(e) {
    const tabCur = e.currentTarget.dataset.index;
    this.setData({
      tabCur
    });
  },

  /**
   * 返回列表
   */
  onBackToList() {
    if (this.data.projectId) {
      wx.navigateBack();
    } else {
      wx.redirectTo({
        url: './research_resource_list'
      });
    }
  },

  /**
   * 分享
   */
  onShareAppMessage() {
    const resource = this.data.resource;
    return {
      title: '资源详情：' + (resource ? resource.RESOURCE_NAME : ''),
      path: '/projects/plm/pages/project/resource/research_resource_detail?id=' + this.data.resourceId
    };
  },

  /**
   * 预览资源
   */
  onPreviewResource() {
    const resource = this.data.resource;
    if (!resource) return;
    
    try {
      const fileType = resource.RESOURCE_TYPE;
      
      // 根据文件类型选择不同的预览方式
      if (fileType === 'image') {
        this._previewImage(resource);
      } else {
        this._previewFile(resource);
      }
    } catch (err) {
      console.error('[资源预览] 失败:', err);
      pageHelper.showModal('预览失败', '暂不支持此类文件预览');
    }
  },

  /**
   * 预览图片
   */
  async _previewImage(resource) {
    if (!resource.RESOURCE_URL) {
      pageHelper.showModal('预览失败', '找不到图片地址');
      return;
    }
    
    try {
      await wx.previewImage({
        current: resource.RESOURCE_URL,
        urls: [resource.RESOURCE_URL]
      });
    } catch (err) {
      console.error('[图片预览] 失败:', err);
      pageHelper.showModal('预览失败', '图片加载失败');
    }
  },

  /**
   * 预览文件
   */
  async _previewFile(resource) {
    if (!resource.RESOURCE_URL) {
      pageHelper.showModal('预览失败', '找不到文件地址');
      return;
    }
    
    try {
      pageHelper.showLoading('文件加载中...');
      
      await wx.openDocument({
        filePath: resource.RESOURCE_URL,
        showMenu: true
      });
      
      pageHelper.hideLoading();
    } catch (err) {
      pageHelper.hideLoading();
      console.error('[文件预览] 失败:', err);
      pageHelper.showModal('预览失败', '文件加载失败或格式不支持');
    }
  }
}) 